package com.itheima.web.request;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@WebServlet("/rf1")
public class RequestFacade extends HttpServlet {
    /**
     *
     * @param req   获取请求数据
     *        获取请求行的数据
     *              获取虚拟路径:                req.getContextPath()
     *              获取get请求参数:             req.getQueryString()
     *              资源uri(统一资源标识符)       req.getRequestURI()
     *              资源url(统一资源定位符)       req.getRequestURL() = 主机ip+端口+资源uri
     *       获取请求头的数据
     *              获取请求头中name对应的内容     req.getHeader(String name)
     * @param resp  设置响应数据
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println(req);
        //获取虚拟路径:                req.getContextPath()
        System.out.println(req.getContextPath());
        //获取get请求参数:             req.getQueryString()
        System.out.println(req.getQueryString());
        //资源uri(统一资源标识符)       req.getRequestURI()
        System.out.println(req.getRequestURI());
        //资源url(统一资源定位符)       req.getRequestURL() = 主机ip+端口+资源uri
        System.out.println(req.getRequestURL());
        //客户端ip
        String remoteHost = req.getRemoteHost();
        System.out.println("remoteHost = " + remoteHost);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求头数据
        String header = req.getHeader("user-agent");
        System.out.println("header = " + header);

        String agent = req.getHeader("sec-CH-ua");
        System.out.println("agent = " + agent);

        //获取post请求体
        //获取字符输入流
        BufferedReader reader = req.getReader();
        //读取输入
        String s = reader.readLine();
        System.out.println(s.getBytes(StandardCharsets.UTF_8));
    }
}
